package com.ht.common.core.web.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ht.common.core.utils.StringUtils;
import com.ht.common.core.utils.file.FileUtils;

import cn.hutool.core.io.FileUtil;
@RestController
@RequestMapping("/sys")
public class BasicController {
	 @GetMapping(value = "/lgos")
	    public List<String> lgos(){
	    	List<String> logs=new ArrayList<String>();
			String appRoot= System.getProperty("user.dir")+"/logs";
			File[] files=FileUtil.ls(appRoot);
			if(StringUtils.isNotEmpty(files)){
				for (File file : files) {
					logs.add(file.getName());
				}
			}
			return logs;
	    }
	    
	    @GetMapping("lgo")
	    public void lgo(String name,HttpServletResponse response, HttpServletRequest request)
	    {
	        try
	        {
	        	String filePath= System.getProperty("user.dir")+"/logs/"+name;
	            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
	            FileUtils.setAttachmentResponseHeader(response, name);
	            FileUtils.writeBytes(filePath, response.getOutputStream());
	        }
	        catch (Exception e)
	        {
	        }
	    }
}
